package everyday;

import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import com.jcraft.jsch.Session;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * sftp相关操作
 *
 * @author gaojie
 * @date 2024-07-09
 */
public class SftpOperation {

    /**
     * 从sftp下载文件到本地
     * 生产环境：因为网络问题无法连接
     */
    @Test
    void test01() {
        String host = "sftp.llschain.com";
        int port = 20622;
        String username = "app_ZJBeiJingBank_1922168030";
        String password = "Lls20220315app_ZJBeiJingBank_1922168030@081453!";
        Session session = JschUtil.createSession(host, port, username, password);
        Sftp sftp = JschUtil.createSftp(session);
        final List<String> ls = sftp.ls("/");
        System.out.println(ls);
        sftp.close();
        session.disconnect();
    }

    /**
     * 从sftp下载文件到本地
     * qa环境：ok
     */
    @Test
    void test02() {
        String host = "qa.sftp.lls.com";
        int port = 22;
        String username = "app_ZJBeiJingBank_1922168030";
        String password = "Lls20220315app_ZJBeiJingBank_1922168030@081501!";
        String srcPath = "/download/tmp/bums/ZYGJ2024062800006336.pdf";
        String localPath = "D:\\opt\\fheb\\bums";
        Session session = JschUtil.createSession(host, port, username, password);
        Sftp sftp = JschUtil.createSftp(session);
        sftp.get(srcPath, localPath);
        System.out.println("下载完成");
        sftp.close();
        session.disconnect();
    }

    /**
     * 测试遍历sftp目录
     */
    @Test
    void test03() {
        String host = "qa.sftp.lls.com";
        int port = 22;
        String username = "app_ZJBeiJingBank_1922168030";
        String password = "Lls20220315app_ZJBeiJingBank_1922168030@081501!";
        Session session = JschUtil.createSession(host, port, username, password);
        Sftp sftp = JschUtil.createSftp(session);
        printDir(sftp, "/");
        sftp.close();
        session.disconnect();
    }

    /**
     * 遍历指定路径下的全部目录
     */
    void printDir(Sftp sftp, String path) {
        if (sftp.exist(path)) {
            List<String> ls = sftp.ls(path);
            if (ls.size() != 0) {
                ls.forEach(e -> {
                    String dirPath = "/".equals(path) ? path + e : path + "/" + e;
                    if (sftp.isDir(dirPath)) {
                        System.out.println(dirPath);
                        printDir(sftp, dirPath);
                    }
                });
            }
        } else {
            System.err.printf("目录不存在[%s]%n", path);
        }
    }

    /**
     * 测试遍历sftp文件
     */
    @Test
    void test04() {
        String host = "qa.sftp.lls.com";
        int port = 22;
        String username = "app_ZJBeiJingBank_1922168030";
        String password = "Lls20220315app_ZJBeiJingBank_1922168030@081501!";
        Session session = JschUtil.createSession(host, port, username, password);
        Sftp sftp = JschUtil.createSftp(session);
        print(sftp, "/");
        sftp.close();
        session.disconnect();
    }

    /**
     * 遍历指定路径下的全部目录和文件
     */
    void print(Sftp sftp, String path) {
        if (sftp.exist(path)) {
            List<String> ls = sftp.ls(path);
            if (ls.size() != 0) {
                ls.forEach(e -> {
                    String dirPath = "/".equals(path) ? path + e : path + "/" + e;
                    System.out.println(dirPath);
                    if (sftp.isDir(dirPath)) {
                        print(sftp, dirPath);
                    }
                });
            }
        } else {
            System.err.printf("目录不存在[%s]%n", path);
        }
    }
}